/*
* Copyright (c) 2010 Michael Collins
*
* This file is part of TerraFirma.
*
* TerraFirma is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TerraFirma is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TerraFirma.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "task_thread.h"
#include "mpi.h"
#include "cstring"

namespace engine {

    /*
        Because of template instanciation issues, the only function defined in here
        is the non-member function get_task_id_by_name()
    */

    //Will always return a value regardless if the task actually exists or not
    const task_id get_task_id_by_name(const char* name)
    {
        task_id hash = 0;
        //const char* str = t.name();
        size_t len = strlen(name);
        size_t len4 = len & (-4); //Round down to nearest multiple of 4
        for( size_t i = 0; i < len4; i+=4 )
            hash ^= *(const task_id*)(name+i); //Hash 4 bytes at a time
        for( size_t i = 0; i < len - len4; i++)
            hash ^= (*(const task_id*)(name+(i+len4))) << (24 - i*8); //Hash remaining bytes at a time
        return hash;
    }
};
